package com.ych.modules.api.authorInfc.job.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.ych.modules.api.authorInfc.job.entity.*;
import com.ych.modules.base.dbEntity.TJobReceiveEntity;
import com.ych.modules.base.dbEntity.TUserJobEntity;
import com.ych.modules.base.service.ITUserInfoService;
import com.ych.modules.common.entity.query.QueryReceiveJobEntity;
import com.ych.modules.common.entity.query.QueryUserJobEntity;
import com.ych.modules.common.service.UserJobService;
import com.ych.utils.TLMap;
import com.ych.utils.mysqlPageUtil.PageUtils;
import com.ych.utils.serviceReturn.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2021/3/8
 * Time: 17:21
 * Description:
 */
@RestController("apiUserJobController")
@RequestMapping("/api/auth/job")
@Api(tags = {"API——用户任务相关"}, description = "API——用户任务相关")
public class UserJobController {
    @Autowired
    private UserJobService    userJobService;
    @Autowired
    private ITUserInfoService itUserInfoService;

    @PostMapping("/getUserJob")
    @ApiOperation(value = "任务查询", notes = "任务查询")
    public R<TUserJobEntity> getUserJob(@RequestBody String jobId) {
        try {
            TUserJobEntity jobEntity = userJobService.getUserJob(jobId);
            if ("1".equals(jobEntity)) {
                jobEntity = null;
            }
            return new R(0, "成功", jobEntity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listUserJob")
    @ApiOperation(value = "任务列表查询", notes = "任务列表查询")
    public R<Page<TUserJobEntity>> listUserJob(@RequestBody QueryUserJobEntity entity) {
        try {
            entity.setStatus("0");
            Page<TUserJobEntity> page = userJobService.listUserJob(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listPersonJob")
    @ApiOperation(value = "个人任务列表查询", notes = "个人任务列表查询")
    public R<Page<TUserJobEntity>> listPersonJob(@RequestBody QueryUserJobEntity entity) {
        try {
            entity.setUserId(TLMap.getUserId());
            Page<TUserJobEntity> page = userJobService.listUserJob(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/createJob")
    @ApiOperation(value = "创建任务", notes = "创建任务")
    public R<String> createJob(@RequestBody AddJobEntity entity) {
        try {
            //密码校验
            itUserInfoService.validatePayPassword(TLMap.getUserId(), entity.getPassword());

            TUserJobEntity jobEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TUserJobEntity.class);
            jobEntity.setUserId(TLMap.getUserId());
            userJobService.createJob(jobEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/concernJob")
    @ApiOperation(value = "任务关注/取消关注", notes = "任务关注/取消关注")
    public R<String> concernJob(@RequestBody String jobId) {
        try {
            userJobService.concernJob(jobId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }


    @PostMapping("/receiveJob")
    @ApiOperation(value = "领取任务", notes = "领取任务")
    public R<String> receiveJob(@RequestBody ReceiveJobEntity jobEntity) {
        try {
            TJobReceiveEntity receiveEntity = JSONObject.parseObject(JSONObject.toJSONBytes(jobEntity), TJobReceiveEntity.class);
            userJobService.receiveJob(receiveEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/submitReceiveJob")
    @ApiOperation(value = "完成/提交领取的任务", notes = "完成/提交领取的任务")
    public R<String> submitReceiveJob(@RequestBody SubmitReceiveJobEntity entity) {
        try {
            TJobReceiveEntity receiveEntity = JSONObject.parseObject(JSONObject.toJSONBytes(entity), TJobReceiveEntity.class);
            userJobService.submitReceiveJob(receiveEntity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/auditReceiveJob")
    @ApiOperation(value = "审核领取的任务", notes = "审核领取的任务")
    public R<String> auditReceiveJob(@RequestBody AuditReceiveJobEntity entity) {
        try {
            userJobService.auditReceiveJob(entity);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listReceiveEntity")
    @ApiOperation(value = "任务领取列表查询", notes = "任务领取列表查询")
    public R<Page<TJobReceiveEntity>> listReceiveEntity(@RequestBody QueryReceiveJobEntity entity) {
        try {
            PageUtils pageUtils = userJobService.listReceiveEntity(entity);
            return new R(0, "成功", pageUtils);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/getReceiveEntity")
    @ApiOperation(value = "任务领取详情查询", notes = "任务领取详情查询")
    public R<TJobReceiveEntity> getReceiveEntity(@RequestBody String receiveId) {
        try {
            TJobReceiveEntity entity = userJobService.getReceiveEntity(receiveId);
            return new R(0, "成功", entity);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/removeUserJob")
    @ApiOperation(value = "删除自己发布的任务", notes = "删除自己发布的任务")
    public R<String> removeUserJob(@RequestBody String jobId) {
        try {
            userJobService.removeUserJob(jobId);
            return new R(0, "成功", null);
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }

    @PostMapping("/listConcernJob")
    @ApiOperation(value = "查询自己关注的项目", notes = "查询自己关注的项目")
    public R<Page<TUserJobEntity>> listPersonConcernJob(@RequestBody QueryPersonJobEntity entity) {
        try {
            Page<TUserJobEntity> page = userJobService.listPersonConcernJob(entity);
            return new R(0, "成功", new PageUtils(page));
        } catch (Exception e) {
            return new R(1, e.getMessage(), null);
        }
    }
}
